package com.example.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.dao.CourseTeacherDao;
import com.example.domain.CourseTeacher;
import com.example.result.Result;
import com.example.result.ResultFactory;
import com.example.service.CourseTeacherService;
import com.example.tool.DateTool;

@Service
public class CourseTeacherServiceImpl implements CourseTeacherService {

    @Autowired
    CourseTeacherDao courseTeacherDao;

    /**
     * @describe 添加老师课程信息
     */
    @Override
    public Result addCourseTeacher(String json) {
        JSONObject parseObject = JSON.parseObject(json);
        String[] courseId = parseObject.getString("courseId").split(",");
        String[] userId = parseObject.getString("userId").split(",");
        List<CourseTeacher> list = new ArrayList<CourseTeacher>();
        if (courseId != null && userId != null) {
            for (int i = 0; i < courseId.length; i++) {
                CourseTeacher courseTeacher = new CourseTeacher();
                courseTeacher.setCourseId(Integer.parseInt(courseId[i]));
                courseTeacher.setUserId(Integer.parseInt(userId[0]));
                // 获取系统当前时间作为记录创建时间
                courseTeacher.setRecordTime(DateTool.getTimestamp());

                list.add(courseTeacher);
            }

        }
        // 调用JPA保存实体方法
        List<CourseTeacher> courseTeacher1 = courseTeacherDao.saveAll(list);

        if (courseTeacher1 != null) {
            return ResultFactory.buildSuccessResult(courseTeacher1);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 更新老师课程信息
     */
    @Override
    public Result updateCourseTeacher(CourseTeacher courseTeacher) {
        if (courseTeacher != null) {
            // 获取系统当前时间作为记录更新时间
            courseTeacher.setUpdateTime(DateTool.getTimestamp());
        }
        // 调用JPA保存并刷新实体方法
        CourseTeacher courseTeacher1 = courseTeacherDao.saveAndFlush(courseTeacher);

        if (courseTeacher1 != null) {
            return ResultFactory.buildSuccessResult(courseTeacher1);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 根据老师课程编号删除老师课程信息
     */
    @Override
    public Result deleteCourseTeacherById(String courseTeacherId) {
        int row = courseTeacherDao.deleteByCourseTeacherId(Integer.parseInt(courseTeacherId));

        if (row > 0) {
            return ResultFactory.buildDefaultSuccessResult();
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 查询所有老师课程信息
     */
    @Override
    public Result queryAllCourseTeacher() {
        List<CourseTeacher> list = courseTeacherDao.findAll();
        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 根据编号查询老师课程信息
     */
    @Override
    public Result queryCourseTeacherById(String courseTeacherId) {
        List<CourseTeacher> list = courseTeacherDao.findByCourseTeacherId(Integer.parseInt(courseTeacherId));
        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 通过Excel批量添加老师课程信息
     */
    @Override
    public Result addCourseTeacherByExcel(List<CourseTeacher> list) {
        List<CourseTeacher> list1 = null;
        if (list != null) {
            for (int i = 0; i < list.size(); i++) {
                list.get(i).setRecordTime(DateTool.getTimestamp());
            }
            list1 = courseTeacherDao.saveAll(list);
        }
        if (list1 != null) {
            return ResultFactory.buildSuccessResult(list1);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 根据老师课程编号批量删除老师课程信息
     */
    @Override
    public Result deleteCourseTeacherByIds(Map<String, String> ids) {
        List<Integer> listId = new ArrayList<Integer>();
        int row = 0;
        if (ids != null) {
            String idstr = ids.get("Ids");
            String[] id = idstr.split(",");

            for (int i = 0; i < id.length; i++) {
                listId.add(Integer.parseInt(id[i]));

            }
        }
        System.out.println(listId);
        if (listId != null) {
            row = courseTeacherDao.deleteByCourseTeacherIdIn(listId);
        }

        if (row > 0) {
            return ResultFactory.buildDefaultSuccessResult();
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 老师课程信息模糊查询
     */
    @Override
    public Result serachByInfo(String[] info) {
        List<Map<String, Object>> list = null;

        if (info[0].equals("userName")) {

            list = courseTeacherDao.findByUserNameLike(info[1]);

        } else if (info[0].equals("nickName")) {

            list = courseTeacherDao.findByNickNameLike(info[1]);

        } else if (info[0].equals("courseName")) {

            list = courseTeacherDao.findByCourseNameLike(info[1]);

        }
        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 查询所有未分配课程的老师
     */
    @Override
    public Result queryAllTeachers() {
        List<Map<String, Object>> list = courseTeacherDao.queryAllTeachers();
        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 查询所有课程名称
     */
    @Override
    public Result queryAllCourseName() {
        List<Map<String, Object>> list = courseTeacherDao.queryAllCourseName();
        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 查询所有已分配的教师课程信息
     */
    @Override
    public Result queryAllTeacherAndCourseInfo() {
        // TODO 查询所有已分配的教师课程信息
        List<Map<String, Object>> list = courseTeacherDao.queryAllTeacherAndCourseInfo();
        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

}
